﻿
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Toolkit.Converter
{
    /// <summary>
    /// 用于IFormattable格式化转换
    /// </summary>
    public class FormattableConverter : ConverterBase<IFormattable, string>
    {

        public string Format { set; get; }

        public string Sp { set; get; }

        public FormattableConverter()
        {
            Sp = "~";
        }

        protected override string ConvertNotNull(IFormattable value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string fm = null;
            if(parameter != null)
            {
                fm = parameter.ToString();
            }
            else
            {
                fm = Format;
            }
            string[] str = fm.ToString().Split(Sp.ToCharArray());
            fm = str[0];
            return value.ToString(fm, culture);
        }

        protected override bool IsNull(object value, object parameter, System.Globalization.CultureInfo culture)
        {
            if(base.IsNull(value, parameter, culture))
            {
                return true;
            }
            if(value.GetType() == typeof(int))
            {
                return ((int)value) == int.MinValue;
            }else if(value.GetType()== typeof(double))
            {
                return ((double)value) == double.MinValue;
            }else if(value.GetType()  == typeof(DateTime))
            {
                return ((DateTime)value) == DateTime.MinValue;
            }
            return false;
        }

        protected override string GetConvertNullValue(Type targetType, object parameter)
        {
            if (parameter != null)
            {
                string[] str = parameter.ToString().Split(new char[] { ':' });
                if(str.Length >=2)
                {
                    return str[1];
                }
            }
            return  "";
        }

        protected override IFormattable ConvertBackNotNull(string value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
